import random

from yysautomatic.log import get_logger
from yysautomatic.core.script import Script
from .listener import click_tiaozhan, click_jiesuan

logger = get_logger(__name__)


class HunTuScript(Script):
    """
    挖土脚本
    """
    name = '自动魂土'
    __count_total = 0
    __count_success = 0
    __count_fail = 0

    def __init__(self, frame_manager):
        self.__frame_manager = frame_manager

    def start(self):
        logger.debug('魂土脚本启动...')
        frames = self.__frame_manager.frames
        for frame in frames:
            frame.label_jiesuan3.on_show(self.on_jiesuan_show)
            frame.label_jiesuan4.on_show(self.on_jiesuan_show)
            frame.button_tiaozhan.on_show(self.on_button_tiaozhan_show)
        logger.info('魂土脚本启动成功...')

    def stop(self):
        logger.debug('魂土脚本停止...')
        frames = self.__frame_manager.frames
        for frame in frames:
            frame.label_jiesuan.remove_listener(self.on_jiesuan_show)
            frame.button_tiaozhan.remove_listener(self.on_button_tiaozhan_show)
        logger.info('魂土脚本停止成功...')

    def on_jiesuan_show(self, event):
        logger.info('魂土挑战成功')
        click_jiesuan(event)
        self.__count_success += 1

    def on_button_tiaozhan_show(self, event):
        self.__count_total += 1
        click_tiaozhan(event)
        logger.info('组队挑战开始')
